﻿#include "bestfreight.h"
#include "ui_bestfreight.h"

BestFreight::BestFreight(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BestFreight)
{
    ui->setupUi(this);

    table=TD_BEST;
    overTable=TD_BEST_OVER;

    updateProvinces();
}

BestFreight::~BestFreight()
{
    delete ui;
}

void BestFreight::updateProvinces()
{
    ui->provinces->clear();

    QStringList provinces;
    QSqlQuery query;
    query.exec(QString("select province from %1 group by province")
               .arg(table));
    while (query.next()) {
        provinces.append(query.value(0).toString());
    }

    if(!provinces.isEmpty())
        ui->provinces->addItems(provinces);
}

void BestFreight::on_btn_calc_clicked()
{
    QString city=ui->cities->currentText();
    QString mass=ui->mass->text().trimmed();
    if(city.isEmpty() || mass.isEmpty())
        return;

    bool ok;
    double dMass=mass.toDouble(&ok);
    if(!ok)
    {
        QMessageBox::warning(this,QStringLiteral("无效输入"),
                             QStringLiteral("请输入正确的数字！"));
        return;
    }
    else
        updateView(city,dMass);
}

void BestFreight::updateView(const QString &city, double mass)
{
    double total_price=0;
    QString summary;

    QSqlQuery query;
    if(mass > 1000)
    {
        query.exec(QString("select level_2 from %1 where city='%2'")
                   .arg(overTable)
                   .arg(city));
        if(query.next())
        {
            double price=query.value(0).toDouble();
            total_price = mass * price;
            summary=QStringLiteral("1000公斤以上，") + QString::number(mass) + QString(" * ") + QString::number(price);
        }
        else
            summary=QStringLiteral("找不到相应地址！");
    }
    else if(mass > 300)
    {
        query.exec(QString("select level_1 from %1 where city='%2'")
                   .arg(overTable)
                   .arg(city));
        if(query.next())
        {
            double price=query.value(0).toDouble();
            total_price = mass * price;
            summary= QStringLiteral("301~1000公斤，") + QString::number(mass) + QString(" * ") + QString::number(price);
        }
        else
            summary=QStringLiteral("找不到相应地址！");
    }
    else if(mass > 70)
    {
        query.exec(QString("select level_0,floor from %1 where city='%2'")
                   .arg(overTable)
                   .arg(city));
        if(query.next())
        {
            double price=query.value(0).toDouble();
            total_price = mass * price;
            summary= QStringLiteral("71~300公斤，") + QString::number(mass) + QString(" * ") + QString::number(price);
        }
        else
            summary=QStringLiteral("找不到相应地址！");
    }
    else
    {
        query.exec(QString("select first,last,step from %1 where city='%2'")
                   .arg(table)
                   .arg(city));
        if(query.next())
        {
            double first=query.value("first").toDouble();
            double last=query.value("last").toDouble();
            int step=query.value("step").toInt();
            if(mass <= step)
            {
                total_price=first;
                summary=QString::number(step) + QStringLiteral("公斤内为起步价");
            }
            else
            {
                total_price=first + (mass - step) * last;
                summary=QStringLiteral("首重(") + QString::number(first) + QString(")") +
                        QString(" + (") +
                        QString::number(mass) + QString(" - ") + QString::number(step)  + QString(") * ") +
                        QStringLiteral("续重(") + QString::number(last) + QString(")");
            }
        }
        else
            summary=QStringLiteral("找不到相应地址！");
    }

    ui->lbl_display->setText(city + QString(" ") + QString::number(mass)
                             + QStringLiteral(" 公斤"));
    ui->lbl_total->setText(QStringLiteral("【运费】 ") + QString::number(total_price));
    ui->lbl_summary->setText(summary);
    ui->mass->clear();
}

void BestFreight::on_provinces_currentTextChanged(const QString &province)
{
    if(!province.isEmpty())
    {
        ui->cities->clear();

        QStringList cities;
        QSqlQuery query;
        query.exec(QString("select city from %1 where province='%2'")
                   .arg(table)
                   .arg(province));
        while (query.next()) {
            cities.append(query.value(0).toString());
        }
        if(!cities.isEmpty())
            ui->cities->addItems(cities);
    }
}
